package com.example.carrierapp.ui.main.goodssource;

import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.example.carrierapp.base.BaseFragment;
import com.example.carrierapp.ui.adapter.FindGoodsSourceMainAdapter;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 找货源
 */
public class FindGoodsSourceFragment extends BaseFragment {
    @BindView(R.id.vp_container)
    ViewPager mViewPager;
    @BindView(R.id.toolbar)
    RelativeLayout toolbar;
    @BindView(R.id.toolbar_radio_group)
    RadioGroup toolbarRadGro;
    @BindView(R.id.toolbar_radio_button_left)
    RadioButton toolbarRadBtnL;
    @BindView(R.id.toolbar_radio_button_right)
    RadioButton toolbarRadBtnR;

    public static FindGoodsSourceFragment newInstance() {
        return new FindGoodsSourceFragment();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_find_gooods_source;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this, mRootView);

        initTitle();
        initAdapter();
        initEventt();
    }

    private void initAdapter() {
        FindGoodsSourceMainAdapter mAdapter = new FindGoodsSourceMainAdapter(getChildFragmentManager(), getResources().getStringArray(R.array.title_goods_main));
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
    }

    private void initTitle() {
        toolbarRadBtnL.setText("货源大厅");
        toolbarRadBtnR.setText("竞抢记录");
    }

    public void initEventt() {
        super.initEvent();
        toolbarRadGro.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.toolbar_radio_button_left:
                    mViewPager.setCurrentItem(0);
                    break;
                case R.id.toolbar_radio_button_right:
                    mViewPager.setCurrentItem(1);
                    break;
                default:
                    break;
            }

        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        toolbarRadGro.check(R.id.toolbar_radio_button_left);
                        break;
                    case 1:
                        toolbarRadGro.check(R.id.toolbar_radio_button_right);
                        break;
                    default:
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
